package com.lee.reply.service.impl;

import com.lee.api.client.ArticleClient;
import com.lee.common.exception.ApplicationException;
import com.lee.common.model.AppResult;
import com.lee.common.model.ArticleReply;
import com.lee.common.model.ResultCode;
import com.lee.common.utils.StringUtil;

import com.lee.reply.mapper.ArticleReplyMapper;
import com.lee.reply.service.IArticleReplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-10-06
 * Time: 11:12
 */
@Slf4j
@Service
public class ArticleReplyServiceImpl implements IArticleReplyService {
    @Resource
    private ArticleReplyMapper articleReplyMapper;

    @Resource
    private ArticleClient articleService;

    @Override
    public void create(ArticleReply articleReply) {
        // 1.校验参数
        if (articleReply == null || articleReply.getArticleId() == null
        || articleReply.getPostUserId() == null
        || StringUtil.isEmpty(articleReply.getContent())) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 2.设置默认值
        articleReply.setReplyId(null); // 设置编号
        articleReply.setReplyUserId(null); // 设置楼中楼
        articleReply.setLikeCount(0); // 设置点赞数
        articleReply.setState((byte) 0); // 设置回复状态
        articleReply.setDeleteState((byte) 0); // 设置删除状态
        Date date = new Date();
        articleReply.setCreateTime(date); // 设置创建日期
        articleReply.setUpdateTime(date); // 设置更新日期
        // 3.调用DAO
        int row = articleReplyMapper.insertSelective(articleReply);
        // 判断是否更新成功
        if (row != 1) {
            // 打印日志
            log.warn(ResultCode.FAILED+"受影响的行数不等于1.");
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
        // 4.更新回复数
        articleService.addOneArticleReplyCountById(articleReply.getArticleId());
        // 5.打印日志
        log.info("回复成功：user id = "+articleReply.getPostUserId() + ",article id = " + articleReply.getArticleId());
    }

    @Override
    public List<ArticleReply> selectByArticleId(Long articleId) {
        // 1.校验参数
        if (articleId == null || articleId <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        // 新增逻辑3：查数据库之前先查redis
//        List<ArticleReply> hotArticleReply = iArticleTopCacheService.getHotArticleReply(articleId);
//        if (hotArticleReply != null && hotArticleReply.size() != 0) {
//            // 如果redis中有数据，直接返回
//            return hotArticleReply;
//            // 否则再去查数据库
//        }

        // 2.调用DAO查询对应主贴的回复列表
        List<ArticleReply> result = articleReplyMapper.selectByArticleId(articleId);
        // 3.返回结果
        return result;
    }
}
